package me.khrystal.rxnews.module.news.presenter;

import java.util.List;
import java.util.Map;

import me.khrystal.rxnews.base.BasePresenterImpl;
import me.khrystal.rxnews.greendao.NewsChannelTable;
import me.khrystal.rxnews.module.news.model.INewsChannelInteractor;
import me.khrystal.rxnews.module.news.model.INewsChannelInteractorImpl;
import me.khrystal.rxnews.module.news.view.INewsChannelView;
import me.khrystal.rxnews.utils.RxBus;

/**
 * usage:
 * author: kHRYSTAL
 * create time: 16/4/29
 * update time:
 * email: 723526676@qq.com
 */
public class INewsChannelPresenterImpl
    extends BasePresenterImpl<INewsChannelView,Map<Boolean,List<NewsChannelTable>>>
    implements INewsChannelPresenter{

    private INewsChannelInteractor<Map<Boolean,List<NewsChannelTable>>> mNewsChannelInteractor;

    private boolean mChannelChange;

    public INewsChannelPresenterImpl(INewsChannelView view) {
        super(view);
        mNewsChannelInteractor = new INewsChannelInteractorImpl();

        mSubscription = mNewsChannelInteractor.channelDbOperate(this,"",null);
    }

    /**
     * 关闭页面时向NewsActivity发送Event 通知另一个页面是否重新读取频道列表
     */
    @Override
    public void onDestory() {
        RxBus.get().post("channelChange",mChannelChange);
        super.onDestory();
    }


    @Override
    public void onItemAddOrRemove(String channelName, boolean selectState) {
        mChannelChange = true;
        mSubscription = mNewsChannelInteractor.channelDbOperate(this,channelName,selectState);
    }



    @Override
    public void onItemSwap(int fromPos, int toPos) {
        mChannelChange = true;
        mSubscription = mNewsChannelInteractor.channelDbSwap(this,fromPos,toPos);
    }

    @Override
    public void requestSuccess(Map<Boolean, List<NewsChannelTable>> data) {
        //只有初始化时才会调用
        mView.initTwoRecyclerView(data.get(true),data.get(false));
    }
}
